package Servlet;

import util.JsonUtils;
import util.ListObject;
import util.ResponseUtils;
import util.StatusCode;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;


@WebServlet("/Servlet")
public class Query_Servlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String s=req.getParameter("semester");
        System.out.println(s);
        // Dao  层
        Query mysqlDao = new Query();    // 构建 UserDao 实例化对象
        List<Dao> userList =  mysqlDao.query(s);   // 查询

        // 添加数据
        List<HashMap> list = new ArrayList<HashMap>();
        for (int i = 0; i < userList.size() ; i++) {
            HashMap map = new HashMap();
            map.put("name",userList.get(i).getClassNo());
            map.put("num1",userList.get(i).getMale());
            map.put("num2",userList.get(i).getFemale());
            list.add(map);
        }
        System.out.println(list);

        // 封装    json
        ListObject listObject = new ListObject();
        listObject.setCode(StatusCode.CODE_SUCCESS);
        listObject.setItems(list);
        listObject.setMsg("成功");

        try {
            ResponseUtils.renderJson(resp, JsonUtils.toJson(listObject));

        }catch (Exception e){
            e.printStackTrace();
        }

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doPost(req, resp);
    }
}
